<template>
	<view class="index_content_basic">
		<view class="index_content_basic_main">
			<el-form :model="ruleForm" :rules="rules" label-position="top" ref="ruleForm" label-width="100px"
				class="demo-ruleForm">
				<el-form-item label="照片" prop="photoUrl">
					<UploadImage :data="upLoadImages" @uploadImageClick="uploadImageClicks_" />
					<!-- <img :src="imageUrl" alt="">
					<view class="flex aic jcsb" style="line-height: 20px;">
						<el-button class="blue_btn" style="" type="primary">选择上传图片</el-button>
						<view class="blue_btn_p" style="width: calc(100% - 135px);">
							<text class="gray fonts12 ">请上传1寸照，文件大小不超过2M，支持jpg、jpeg、png格式</text>
						</view>
					</view> -->
				</el-form-item>
				<el-form-item label="姓名" prop="fullName">
					<el-input v-model="ruleForm.fullName" placeholder="请输入姓名"></el-input>
				</el-form-item>
				<el-form-item label="性别" prop="gender">
					<el-select v-model="ruleForm.gender" placeholder="请选择性别" style="width: 100%;">
						<el-option v-for="item in genderOption" :label="item.dictLabel" :value="item.dictValue" v-if="item.dictValue!='-1'">
						</el-option>
					</el-select>
				</el-form-item>
				<el-form-item label="身份证号" prop="idNumber">
					<el-input v-model="ruleForm.idNumber" @blur="calculateAge()" placeholder="请输入身份号"></el-input>
				</el-form-item>
				<el-form-item label="出生年月" prop="">
					<el-date-picker type="date" disabled format="yyyy-MM-dd" value-format="yyyy-MM-dd HH:mm:ss"
						placeholder="请选择出生年月" v-model="ruleForm.brithDay" style="width: 100%;">
					</el-date-picker>
				</el-form-item>
				<el-form-item label="年龄" prop="">
					<el-input v-model="ruleForm.age" disabled placeholder="请输入年龄"></el-input>
				</el-form-item>
				<el-form-item label="联系电话" prop="phoneNumber">
					<el-input v-model="ruleForm.phoneNumber" placeholder="请输入联系电话"></el-input>
				</el-form-item>
				<el-form-item label="电子邮箱" prop="email">
					<el-input v-model="ruleForm.email" placeholder="请输入电子邮箱"></el-input>
				</el-form-item>
				<el-form-item label="参加工作时间" prop="">
					<el-date-picker type="date" format="yyyy-MM-dd" value-format="yyyy-MM-dd HH:mm:ss" placeholder="请选择参加工作时间"
						v-model="ruleForm.startWorkTime" style="width: 100%;">
					</el-date-picker>
				</el-form-item>
				<el-form-item label="紧急联系人及电话" prop="">
					<el-input v-model="ruleForm.emergencyPhoneNumber" placeholder="请输入紧急联系人及电话"></el-input>
				</el-form-item>
				<el-form-item label="籍贯" prop="">
					<el-input v-model="ruleForm.nativePlace" placeholder="请输入籍贯"></el-input>
				</el-form-item>
				<el-form-item label="政治面貌" prop="politicalOutlook">
					<el-select v-model="ruleForm.politicalOutlook" placeholder="请选择政治面貌" style="width: 100%;">
						<el-option v-for="item in politicalOutlookOption" :label="item.dictLabel"
							:value="item.dictValue" v-if="item.dictValue!='-1'">
						</el-option>
					</el-select>
				</el-form-item>
				<el-form-item label="婚姻" prop="">
					<el-select v-model="ruleForm.marriage" placeholder="请选择婚姻" style="width: 100%;">
						<el-option v-for="item in marriageOption" :label="item.dictLabel" :value="item.dictValue">
						</el-option>
					</el-select>
				</el-form-item>
				<el-form-item label="健康状态" prop="">
					<el-input v-model="ruleForm.health" placeholder="请输入健康状态"></el-input>
				</el-form-item>
				<el-form-item label="现住地址" prop="currentAddress">
					<el-input v-model="ruleForm.currentAddress" placeholder="请输入现住地址"></el-input>
				</el-form-item>
				<el-form-item label="户籍地址（身份证地址）" prop="registeredAddress">
					<el-input v-model="ruleForm.registeredAddress" placeholder="请输入户籍地址（身份证地址）"></el-input>
				</el-form-item>
				<el-form-item label="熟悉专业 有何专长（示例）" prop="">
					<el-input v-model="ruleForm.majorAndSpecialty" placeholder="请输入专业"></el-input>
				</el-form-item>
				<el-form-item label="目前年薪" prop="currentAnnualSalary">
					<el-input v-model="ruleForm.currentAnnualSalary" placeholder="税前">
						<text slot="suffix" class="fonts12">万/年</text>
					</el-input>
				</el-form-item>
				<el-form-item label="重点人群" prop="keyPopulationInformation">
					<el-select v-model="ruleForm.keyPopulationInformation" placeholder="请选择" style="width: 100%;">
						<el-option v-for="item in keyPopulationInformationOption" :label="item.dictLabel"
							:value="item.dictValue"></el-option>
					</el-select>
				</el-form-item>
				<el-form-item label="现工作单位及职务" prop="currentWorkUnit">
					<el-input v-model="ruleForm.currentWorkUnit" placeholder="请输入现工作单位及职务"></el-input>
				</el-form-item>
				<!-- <el-form-item label="现任职务" prop="currentPosition">
					<el-input v-model="ruleForm.currentPosition" placeholder="请输入现任职务"></el-input>
				</el-form-item> -->
				<el-form-item label="是否服从调剂" prop="obeyAdjustment">
					<el-select v-model="ruleForm.obeyAdjustment" placeholder="请选择是否服从调剂" style="width: 100%;">
						<el-option v-for="item in sys_yes_noOption" :label="item.dictLabel" :value="item.dictValue">
						</el-option>
					</el-select>
				</el-form-item>
			</el-form>
		</view>
		<view class="position_footer flex aic jcsb">
			<el-button class="blue_btn" style="width: 100%;" type="primary" @click="submitForm('ruleForm')">保存
			</el-button>
		</view>
	</view>
</template>

<script>
	import {
		editResume
	} from "@/api/my.js"
	import UploadImage from "@/pages/components/UploadImage.vue"
	export default {
		components: {
			UploadImage
		},
		data() {
			var checkEmail = (rule, value, callback) => { //邮箱校验
				const mailReg = /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(.[a-zA-Z0-9_-])+/
				if (!value) {
					return callback(new Error('邮箱不能为空'))
				}
				setTimeout(() => {
					if (mailReg.test(value)) {
						callback()
					} else {
						callback(new Error('请输入正确的邮箱格式'))
					}
				}, 100)
			};
			var checkPhone = (rule, value, callback) => {
				if (!value) {
					return callback(new Error("手机号不能为空"));
				} else {
					const reg = /^1[3-9][0-9]\d{8}$/;
					// 这里定义的reg变量没有加引号，所以是一种特殊的数据类型，即正则，可以直接用test()方法
					if (reg.test(value)) {
						callback();
					} else {
						// 具体错误信息没有提示
						return callback(new Error("请输入正确的手机号"));
					}
				}
			};
			return {
				upLoadImages: {
					tip: "请上传1寸照，文件大小不超过2M，支持jpg、jpeg、png格式,建议尺寸259x413",
					url: "",
				},
				imageUrl: "",
				ruleForm: {
					fullName: "",
				},
				rules: {
					photoUrl: [{
						required: true,
						message: '请上传照片',
						trigger: 'blur'
					}, ],
					fullName: [{
						required: true,
						message: '请输入姓名',
						trigger: 'blur'
					}, ],
					gender: [{
						required: true,
						message: '请选择性别',
						trigger: 'change'
					}],
					politicalOutlook: [{
						required: true,
						message: '请选择政治面貌',
						trigger: 'change'
					}],
					keyPopulationInformation: [{
						required: true,
						message: '请选择重点人群',
						trigger: 'change'
					}],
					obeyAdjustment: [{
						required: true,
						message: '请选择是否服从调剂',
						trigger: 'change'
					}],
					currentAddress: [{
						required: true,
						message: '请输入现住地址',
						trigger: 'blur'
					}, ],
					registeredAddress: [{
						required: true,
						message: '请输入户籍地址',
						trigger: 'blur'
					}, ],
					currentWorkUnit: [{
						required: true,
						message: '请输入现工作单位及职务',
						trigger: 'blur'
					}, ],
					currentAnnualSalary: [{
						required: true,
						message: '请输入目前年薪',
						trigger: 'blur'
					}, ],
					
					email: [{
						required: true,
						message: '请输入电子邮箱',
						trigger: 'blur'
					}, {
						validator: checkEmail,
						trigger: "blur"
					}],
					phoneNumber: [{
						required: true,
						message: '请输入手机号',
						trigger: 'blur'
					}, {
						validator: checkPhone,
						trigger: "blur"
					}],
					idNumber: [{
						required: true,
						message: '请输入身份证号',
						trigger: 'blur'
					}, {
						pattern: /^\d{17}(\d|x)$/i,
						message: '身份证号格式不正确',
						trigger: 'blur'
					}],

				},
				politicalOutlookOption: []
			};
		},
		onLoad(options) {
			this.politicalOutlookOption = uni.getStorageSync("political_outlook")
			this.genderOption = uni.getStorageSync("sys_user_sex")
			this.marriageOption = uni.getStorageSync("marriage")
			this.keyPopulationInformationOption = uni.getStorageSync("key_population_information")
			this.sys_yes_noOption = uni.getStorageSync("sys_yes_no")
			this.datas = JSON.parse(options.data)
			this.info(this.datas)
		},
		methods: {
			calculateAge() { //身份证号解析
				let idNumber = this.ruleForm.idNumber
				const year = idNumber.substring(6, 10);
				const month = idNumber.substring(10, 12); // JavaScript 中月份从0开始，需要减1  
				const day = idNumber.substring(12, 14);
				const birthDate = new Date(year, month, day);
				const currentDate = new Date();
				let age = currentDate.getFullYear() - year;
				const m = currentDate.getMonth() - birthDate.getMonth();
				// if (m < 0 || (m === 0 && currentDate.getDate() < birthDate.getDate())) {
				//     age--;
				// }
				this.ruleForm.brithDay = `${year}-${month<10?'0'+month:month}-${day}` + ' 00:00:00';
				this.ruleForm.age = age;
			},
			info(data) {
				this.ruleForm = data.personalInformation ? data.personalInformation : {}
				if (data.personalInformation) {
					this.upLoadImages.url = data.personalInformation.photoUrl ? data.personalInformation.photoUrl : ""
				}

			},
			uploadImageClicks_(value) {
				console.log("图片携带信息", value);
				this.ruleForm.photo = value.ossId
				this.ruleForm.photoUrl = value.url
			},
			submitForm(formName) {
				this.$refs[formName].validate((valid) => {
					if (valid) {
						let obj = this.datas
						console.log(obj);
						obj.personalInformation = this.ruleForm
						editResume(obj)
					} else {
						console.log('error submit!!');
						this.$message({
							message: '请完善信息',
							type: 'error',
							offset: 0.1
						});
						return false;
					}
				});
			},
			resetForm(formName) {
				this.$refs[formName].resetFields();
			}
		}
	}
</script>

<style lang="scss" scoped>
	.blue_btn {
		height: 40px;
		background: #2F6CBC;
		border: 1px solid #2F6CBC;
	}
</style>
